package com.example.yin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.yin.mapper.BannerMapper;
import com.example.yin.model.domain.Banner;
import com.example.yin.service.BannerService;
import com.example.yin.utils.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author asus
 * @description 针对表【banner】的数据库操作Service实现
 * @createDate 2022-06-13 13:13:42
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner>
        implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Cacheable(value = "banner", key = "'list'")  //放在缓存中 redis 是以key-value进行存储的
    @Override
    public List<Banner> getAllBanner() {
        System.out.println("没有走缓存");
        return bannerMapper.selectList(null);
    }

    @Override
    public List<Banner> getKuGouBanner() {
        String KuGouBannerUrl="http://localhost:3000/pc/diantai";
        String response = HttpClientUtils.sendGet(KuGouBannerUrl);

        JSONObject jsonObject = JSON.parseObject(response);
        // 获取
        List<Banner> bannerList=new ArrayList<Banner>();
        JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
        jsonArray.forEach((Object jsonItem) -> {
            if (jsonItem instanceof JSONObject) {
                JSONObject jsonObjectItem = (JSONObject) jsonItem;
                Banner banner = new Banner();
                banner.setId(null);
                banner.setPic(jsonObjectItem.getString("code"));
                bannerList.add(banner);
            } else {
                System.out.println("jsonItem 不是 JSONObject 类型");
            }
        });

        return bannerList;
    }

}
